package com.team.teamproject.ui.classfiy;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.team.teamproject.R;
import com.team.teamproject.adapter.MyPagerFragmentAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 分类首页
 */

public class FragmentFenLei extends Fragment {
    private TabLayout mTablayout;
    private ViewPager vp;
    private EditText etSearch;
    private List<Fragment> fragmentLists;//存放viewPager的fragment
    private List<String> titles;
    private MyPagerFragmentAdapter pagerAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fenlei, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initFragment();
        tabViewPager();
    }

    /**
     * 往viewPager中添加fragment
     */
    private void initFragment() {
        //得到攻略fragment的对象
        StrategyFragment fragment = new StrategyFragment();
        fragmentLists.add(fragment);
        SingleItemFragment fragment1 = new SingleItemFragment();
        fragmentLists.add(fragment1);
    }

    /**
     * TabLayout与ViewPager关联
     */
    private void tabViewPager() {
        //设置TabLayout与ViewPager关联
        mTablayout.setupWithViewPager(vp, true);
        //设置导航标签的颜色
        mTablayout.setTabTextColors(Color.BLACK, Color.RED);
        //设置viewPager的适配器
        pagerAdapter = new MyPagerFragmentAdapter(getChildFragmentManager(), titles, fragmentLists);
        vp.setAdapter(pagerAdapter);
    }

    /**
     * 初始化组件
     *
     * @param view
     */
    private void initView(View view) {
        mTablayout = (TabLayout) view.findViewById(R.id.category_tbl);
        vp = (ViewPager) view.findViewById(R.id.category_vp);
        etSearch = (EditText) view.findViewById(R.id.category_search);
        fragmentLists = new ArrayList<>();
        titles = new ArrayList<>();
        //添加导航标签
        titles.add("攻略");
        titles.add("单品");
    }

}
